package com.meida.module.system.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meida.common.constants.CommonConstants;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.GetModel;
import com.meida.common.mybatis.model.ListModel;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.model.SaveModel;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.system.client.entity.SysHtml;
import com.meida.module.system.provider.mapper.SysHtmlMapper;
import com.meida.module.system.provider.service.SysHtmlService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 服务协议 实现类
 *
 * @author flyme
 * @date 2019-07-13
 */
@Service
public class SysHtmlServiceImpl extends BaseServiceImpl<SysHtmlMapper, SysHtml> implements SysHtmlService {


    @Override
    public SysHtml findByCode(String htmlCode) {
        QueryWrapper queryWrapper = new QueryWrapper<SysHtml>();
        queryWrapper.eq("htmlCode", htmlCode);
        queryWrapper.eq("htmlStatus", 1);
        return getOne(queryWrapper);
    }


    @Override
    public ResultBody listEntityMap(ListModel model) {
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(model, SysHtml.class);
        EntityMap params = cq.getRequestMap();
        cq.select(SysHtml.class, "htmlId", "parentId", "htmlTitle", "htmlContent");
        cq.eq("parentId", 0);
        cq.eq("htmlType", 1);
        return baseList(cq);
    }

    @Override
    public ResultBody add(SaveModel model) {
        CriteriaSave cs = new CriteriaSave(model, SysHtml.class);
        SysHtml html = cs.getEntity();
        String htmlTitle = html.getHtmlTitle();
        ApiAssert.isNotEmpty("分类名称不能为空", htmlTitle);
        SysHtml prodCategory = getHtml(htmlTitle);
        ApiAssert.isEmpty("分类已存在", prodCategory);

        Long parentId = FlymeUtils.isEmpty(html.getParentId()) ? 0L : html.getParentId();
        SysHtml parent = getById(parentId);
        Integer count;
        CriteriaQuery cq = new CriteriaQuery(SysHtml.class);
        if (parentId.equals(0L)) {
            count = count(cq.eq("parentId", cs.getParams("parentId")));
        } else {
            count = count(cq.eq("parentId", parent.getHtmlId()));
        }
        BigDecimal sortOrder = FlymeUtils.getBigDecimal(new BigDecimal(count), "0").add(new BigDecimal(1));

        cs.put("parentId", parentId);
        cs.put("sortOrder", FlymeUtils.isEmpty(html.getSortOrder()) ? new BigDecimal("0") : html.getSortOrder());
        cs.put("htmlTitle", "分类名称" + sortOrder);
        cs.put("sortOrder", sortOrder);
        cs.put("htmlStatus", CommonConstants.ENABLED);

        return baseAdd(cs);
    }

    public SysHtml getHtml(String htmlTitle) {
        CriteriaQuery<SysHtml> cq = new CriteriaQuery(SysHtml.class);
        cq.eq(true, "htmlTitle", htmlTitle);
        return getOne(cq);
    }

    @Override
    public ResultBody get(GetModel model) {
        CriteriaQuery<SysHtml> cq = new CriteriaQuery(model, SysHtml.class);
        cq.select(SysHtml.class, "*");
        SysHtml sysHtml = cq.getEntity(SysHtml.class);
        cq.lambda().or(e -> e.eq(true, SysHtml::getHtmlTitle, sysHtml.getHtmlTitle()).or().eq(true, SysHtml::getHtmlCode, sysHtml.getHtmlCode()));
        return baseGet(cq);
    }
}
